package com.gold.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gold.entity.Game;
import com.gold.entity.GameType;
import com.gold.mapper.GameMapper;
import com.gold.mapper.GameTypeMapper;
import com.gold.myexception.Myexception;
import com.gold.service.GameTypeService;

@Service
public class GameTypeServiceImpl implements GameTypeService {

	@Autowired
	private GameTypeMapper gtm;
	@Autowired
	private GameMapper gm;

	public void insertGameType(Map<String, String> map) throws Myexception {
		String typeName = map.get("typeName");
		int typeStatusId = Integer.parseInt(map.get("typeStatusId"));
		String typePicture = map.get("typePicture");
		List<GameType> gameTypeList = gtm.selectAllGameType();
		if (gameTypeList != null) {
			for (GameType gameType : gameTypeList) {
				if (typeName.equals(gameType.getTypeName())) {
					throw new Myexception("游戏类型名不可重复");
				}
			}
		}
		GameType gameType = new GameType(0, typeName, typeStatusId, "", typePicture, null, null);
		gtm.insertGameType(gameType);
	}

	public List<GameType> selectAllGameType() {
		return gtm.selectAllGameType();
	}

	public List<GameType> selectGameTypeByLikeAndPage(Map<String, String> map) {
		String typeName = map.get("typeName");
		if (typeName == null) {
			map.put("typeName", "");
		}
		int pageSize = 10;
		int userCount = gtm.selectGameTypeByLike(map).size();
		int pageCount = userCount % pageSize == 0 ? userCount / pageSize : userCount / pageSize + 1;
		String nowPageString = map.get("nowPageString");
		if (nowPageString == null) {
			nowPageString = "1";
		}
		int nowPage = Integer.parseInt(nowPageString);
		if (nowPage <= 0) {
			nowPage = pageCount;
		}
		if (nowPage > pageCount) {
			nowPage = 1;
		}
		map.put("start", "" + ((nowPage - 1) * pageSize + 1));
		map.put("end", "" + (nowPage * pageSize));
		map.put("nowPageString", nowPage + "");
		return gtm.selectGameTypeByLikeAndPage(map);
	}

	public List<GameType> selectGameTypeByPage(Map<String, String> map) {
		int pageSize = 3;
		int userCount = gtm.selectAllGameType().size();
		int pageCount = userCount % pageSize == 0 ? userCount / pageSize : userCount / pageSize + 1;
		String nowPageString = map.get("nowPageString");
		if (nowPageString == null) {
			nowPageString = "1";
		}
		int nowPage = Integer.parseInt(nowPageString);
		if (nowPage <= 0) {
			nowPage = pageCount;
		}
		if (nowPage > pageCount) {
			nowPage = 1;
		}
		map.put("start", "" + ((nowPage - 1) * pageSize + 1));
		map.put("end", "" + (nowPage * pageSize));
		map.put("nowPageString", nowPage + "");
		return gtm.selectGameTypeByPage(map);
	}

	public void updateGameType(Map<String, String> map) {
		int typeId = Integer.parseInt(map.get("typeId"));
		int typeStatusId = Integer.parseInt(map.get("typeStatusId"));
		GameType gameType = new GameType(typeId, null, typeStatusId, null, null, null, null);
		gtm.updateGameType(gameType);
	}

	public void deleteGameType(String[] typeId) throws Myexception {
		String exMsg = "";
		List<Game> gameList = gm.selectAllGame();
		for (int i = 0; i < typeId.length; i++) {
			boolean flag = true;
			int tid = Integer.parseInt(typeId[i]);
			if (gameList != null && gameList.size() > 0) {
				for (Game game : gameList) {
					if (game.getGameTypeId() == tid) {
						String typeName = gtm.selectGameTypeByTypeId(tid).getTypeName();
						exMsg += typeName + "删除失败，该类型下存在游戏 |";
						flag = false;
					}
				}
			}
			if (flag) {
				gtm.deleteGameType(tid);
			}
		}
		if (!"".equals(exMsg)) {
			throw new Myexception(exMsg);
		}
	}

	public GameType selectGameTypeByTypeId(String typeId) {
		return gtm.selectGameTypeByTypeId(Integer.parseInt(typeId));
	}

	public List<GameType> selectGameTypeByPortal() {
		return gtm.selectGameTypeByPortal();
	}

	public List<GameType> selectGameTypeByPortalMore() {
		return gtm.selectGameTypeByPortalMore();
	}

	public List<GameType> selectAllGameTypeByPortal() {
		return gtm.selectAllGameTypeByPortal();
	}

}
